<template>
	<view class="wrap">
		<js-lang title="login.title" :showBtn="true"></js-lang>
		<view class="logo">
			<image src="/static/common/cqq-img/logo.png"></image>
		</view>
		<view class="list">
			<view class="list-call">
				<u-icon class="u-icon" size="40" name="account"></u-icon>
				<input class="u-input" type="text" v-model="username" maxlength="32"
					:placeholder="$t('login.placeholderAccount')" />
				<!-- <u-checkbox v-model="remember" active-color="#69cbff">{{ $t('login.autoLogin') }}</u-checkbox> -->
			</view>
			<view class="list-call">
				<u-icon class="u-icon" size="40" name="lock"></u-icon>
				<input class="u-input" type="text" v-model="password" maxlength="32"
					:placeholder="$t('login.placeholderPassword')" :password="!showPassword" />
				<image class="u-icon-right"
					:src="'/static/jeesite/login/eye_' + (showPassword ? 'open' : 'close') + '.png'"
					@click="showPass()"></image>
			</view>
			<view class="list-call">
				<u-icon class="u-icon" size="40" name="coupon"></u-icon>
				<input class="u-input" type="text" v-model="validCode" maxlength="4" placeholder="验证码" />
				<u-image class="img-valid-code" width="300rpx" height="90rpx" :src="imgValidCodeSrc"
					@click="refreshImgValidCode()"></u-image>
			</view>
			<!-- 图形验证码 -->





			<!-- <view class="list-call base-url">
				<u-icon class="u-icon" size="40" name="setting" style="padding-right:15rpx;"></u-icon>
				<js-select v-model="baseUrlValue" :items="baseUrlList" placeholder="快速切换服务器地址" @confirm="updateBaseUrl"></js-select>
			</view> -->
		</view>

		<!-- <view class="agreement">
			<u-checkbox v-model="terms">我已阅读并同意</u-checkbox>
			<navigator url="/pages/sys/login/terms" open-type="navigate">《软件隐私保护》</navigator>
		</view> -->
		<view class="button" hover-class="button-hover" @click="submit()"><text>{{ $t('login.loginButton') }}</text>
		</view>
		<!-- <view class="footer">
			<navigator url="forget" open-type="navigate">{{ $t('login.forget') }}</navigator>
			<text>|</text>
			<navigator url="reg" open-type="navigate">{{ $t('login.reg') }}</navigator>
		</view> -->
	</view>
</template>
<script>
/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
import config from '@/common/config.js';
export default {
	data() {
		return {
			username: null,
			password: null,
			showPassword: false,
			remember: true,
			isValidCodeLogin: false,
			validCode: '',
			imgValidCodeSrc: null,
			terms: false,
			baseUrlValue: '',

			realValidCode: '', // 真实的验证码
			canvasWidth: 150,
			canvasHeight: 40,
			system: uni.getSystemInfoSync(),
		};
	},
	onLoad() {

		this.$u.api.index({ deviceBrand: this.system.deviceBrand ? this.system.deviceBrand : this.system.model, }).then(res => {
			
		});

		this.initValidCode();
	},
	methods: {
		showPass() {
			this.showPassword = !this.showPassword;
		},
		refreshImgValidCode(e) {
			if (this.vuex_token == '') {
				this.$u.api.index().then(res => {
					this.imgValidCodeSrc = this.vuex_baseUrl + '/validCode?__sid='
						+ res.sessionid + '&t=' + new Date().getTime();
				});
			} else {
				this.imgValidCodeSrc = this.vuex_baseUrl + '/validCode?__sid='
					+ this.vuex_token + '&t=' + new Date().getTime();
			}
			this.validCode = '';
		},
		submit() {
			if (this.username.length == 0) {
				this.$u.toast('请输入账号');
				return;
			}
			if (this.password.length == 0) {
				this.$u.toast('请输入密码');
				return;
			}

			this.$u.api.login({
				username: this.username,
				password: this.password,
				loginType: 'app'

			})
				.then(res => {
					if (res.code == 200) {
						this.$u.toast('登陆成功');
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/sys/home/index'
								});
							}, 500);
						
					}
					if (res.isValidCodeLogin) {
						this.isValidCodeLogin = true;
						this.refreshImgValidCode();
					}
				});
		},

		// 生成随机验证码
		generateValidCode() {
			const chars = 'abcdefghjkmnpqrstuvwxyz23456789';
			let code = '';
			for (let i = 0; i < 4; i++) {
				code += chars.charAt(Math.floor(Math.random() * chars.length));
			}
			this.realValidCode = code;
			return code;
		},

		// 生成图形验证码
		generateValidCodeImage() {
			const code = this.generateValidCode();
			// 创建canvas元素
			const canvas = document.createElement('canvas') || uni.createCanvasContext();
			canvas.width = this.canvasWidth;
			canvas.height = this.canvasHeight;

			// 使用uView或其他方式绘制验证码
			this.drawValidCodeOnCanvas(code);
		},

		// 在Canvas上绘制验证码
		drawValidCodeOnCanvas(code) {
			// 由于uni-app中Canvas操作较为复杂，我们使用简单的方式生成base64图片
			this.imgValidCodeSrc = this.createValidCodeBase64(code);
		},

		// 创建简单的base64验证码图片
		createValidCodeBase64(code) {
			// 简单的SVG方式生成验证码图片
			const svg = `
      <svg width="${this.canvasWidth}" height="${this.canvasHeight}" xmlns="http://www.w3.org/2000/svg">
        <rect width="100%" height="100%" fill="#f0f0f0"/>
        ${this.generateRandomLines()}
        <text x="50%" y="50%" font-family="Arial" font-size="24" fill="${this.getRandomColor()}" 
              text-anchor="middle" dominant-baseline="middle" transform="rotate(${Math.random() * 10 - 5}, ${this.canvasWidth / 2}, ${this.canvasHeight / 2})">
          ${code.split('').join(' ')}
        </text>
      </svg>
    `;

			return 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg)));
		},

		// 生成干扰线
		generateRandomLines() {
			let lines = '';
			for (let i = 0; i < 5; i++) {
				lines += `<line x1="${Math.random() * this.canvasWidth}" y1="${Math.random() * this.canvasHeight}" 
                      x2="${Math.random() * this.canvasWidth}" y2="${Math.random() * this.canvasHeight}" 
                      stroke="${this.getRandomColor()}" stroke-width="1"/>`;
			}
			return lines;
		},

		// 获取随机颜色
		getRandomColor() {
			const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57', '#ff9ff3', '#54a0ff', '#5f27cd'];
			return colors[Math.floor(Math.random() * colors.length)];
		},


		// 刷新验证码
		refreshImgValidCode() {
			this.generateValidCodeImage();
			this.validCode = ''; // 清空用户输入
		},

		// 初始化验证码
		initValidCode() {
			this.isValidCodeLogin = true; // 默认显示验证码
			this.generateValidCodeImage();
		},



	}
};
</script>
<style lang="scss">
@import 'index.scss';

.logo {
	width: 119px;
	height: 119px;
	background: #f46f1b;
	box-shadow: 0px 2px 10px 2px rgb(250 182 148);
	border-radius: 50%;
	margin: 36px auto 5px auto;
}

.logo image {
	width: 230rpx;
	height: 230rpx;
	border-radius: 50%;
}

.base-url js-select {
	width: 100%;
}

.button {
	margin: 30rpx auto 0;
}

.agreement {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: center;
	color: #666;
}

.footer {
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	text-align: center;
	color: #46afff;
	height: 40rpx;
	line-height: 40rpx;
	font-size: 35rpx;
	margin-top: 60rpx;
}

.footer text {
	font-size: 30rpx;
	margin-left: 25rpx;
	margin-right: 25rpx;
}

.oauth2 {
	display: flex;
	flex-direction: row;
	justify-content: space-around;
	margin: 55rpx 100rpx;

	image {
		height: 100rpx;
		width: 100rpx;
	}
}

.list-call .u-input {
	background: none !important;
	background-color: transparent !important;
}

input:-internal-autofill-selected {
	background: none !important;
}
</style>
